<?php

namespace App\Http\Controllers\Admin\Ca;

use App\Http\Controllers\Admin\BaseController;
use App\Http\Requests\Admin\Ca\CustomerSaveFormRequest;
use App\Repositories\Ca\CustomerRepository;
use App\Utils\ViewHelper;
use Illuminate\Http\Request;

class DayArrivingController extends BaseController
{
    public function __construct()
    {
        $this->set('ca/day-arriving');
    }

    function index(CustomerRepository $rep, Request $request)
    {
        $start = date('Y-m-d');
        $params = $request->all();
        $params['date_type'] = 3;
        $params['date_range'] = $start . ' ~ ' . $start;
        $params['order_field']='yy_time';
        $customers = $rep->adminPager($params);
        return $this->view('index', ['customers' => $customers, 'search' => $params]);
    }

    function export(CustomerRepository $rep, Request $request)
    {
        $params = $request->all();
        $params['view_type'] = 'export';
        $params['date_type'] = 3;
        $params['date_range'] = $start . ' ~ ' . $start;
        $params['order_field']='yy_time';
        return $rep->adminPager($params);
    }

    function create()
    {
        return $this->view('create');
    }

    function save(CustomerSaveFormRequest $request, CustomerRepository $repository)
    {
        $data = $request->all();
        return $repository->save($data, 0);
    }

    function update(CustomerSaveFormRequest $request, CustomerRepository $repository)
    {
        $data = $request->all();
        $id = $request->get('id', 0);
        return $repository->save($data, $id);
    }

    function edit($id, CustomerRepository $repository)
    {
        $result = $repository->detail($id);
        if ($result->isSuccess()) {
            return $this->view('edit')->with('entity', $result->data);
        }
        return ViewHelper::E404($result->msg);
    }

    function detail($id, CustomerRepository $repository, CustomerLogRepository $logRepository)
    {
        $result = $repository->detail($id, true);
        if ($result->isSuccess()) {
            return $this->view('detail')
                ->with('logs', $logRepository->logs($id))
                ->with('entity', $result->data);
        }
        return ViewHelper::E404($result->msg);
    }

    function batchDelete(Request $request, CustomerRepository $repository)
    {
        return $repository->batchDelete($request->post('ids', ''));
    }

    function delete(Request $request, CustomerRepository $repository)
    {
        return $repository->delete($request->post('id', ''));
    }

    function choose_view(Request $request)
    {
        $choose_mode = $request->get('choose_mode', 1);
        $search = $request->all();
        return $this->view('choose', compact('choose_mode', 'search'));
    }

    function choose(CustomerRepository $rep, Request $request)
    {
        return $rep->adminPager($request->all());
    }

    function leaveHospital(CustomerRepository $rep, Request $request)
    {
        return $rep->leaveHospital($request->get('id', 0));
    }

    function arriving(CustomerRepository $rep, Request $request)
    {
        return $rep->arriving($request->get('id', 0), $request->get('yy_doctor_id', 0));
    }

    function onlineArriving(CustomerRepository $rep, Request $request)
    {
        return $rep->onlineArriving($request->get('id', 0));
    }


    function loss(CustomerRepository $rep, Request $request)
    {
        return $rep->loss($request->get('id', 0), $request->get('reason', ''));
    }

    function visit(CustomerRepository $rep, Request $request)
    {
        return $rep->visit($request->get('id', 0), $request->get('hf_time', ''), $request->get('content', ''));
    }

    public function searchRepeat(CustomerRepository $rep, Request $request)
    {
        $customers = $rep->searchRepeat($request->get('kw'));
        return $this->view('search-repeat', compact('customers'));
    }

    function checkMobile(CustomerRepository $rep, Request $request)
    {
        return $rep->checkMobile($request->get('mobile'));
    }

    function checkWeixin(CustomerRepository $rep, Request $request)
    {
        return $rep->checkWeixin($request->get('weixin'));
    }

    function checkName(CustomerRepository $rep, Request $request)
    {
        return $rep->checkName($request->get('name'));
    }

    function hospitalization(CustomerRepository $rep, Request $request)
    {
        return $rep->hospitalization($request->get('id', 0));
    }

    function swt($id, CustomerRepository $rep)
    {
        $result = $rep->detail($id);
        if ($result->isSuccess()) {
            return $this->view('swt')->with('entity', $result->data);
        }
        return ViewHelper::E404($result->msg);
    }

    function spend($id,$is_spend, CustomerRepository $rep){
        return $rep->spend($id,$is_spend);
    }
}